<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function Subject() {
            let listener = null
            //发布
            return {
                publish(val) {
                    if (listener) listener(val)
                },
                //订阅
                subscrible(cb) {
                    listener = cb
                }

            }

        }
        const subject = Subject()
        subject.subscrible((msg) => {
            console.log(msg);
        })

        setTimeout(() => {
            setTimeout(() => {
                setTimeout(() => {
                    subject.publish('你是个傻逼')
                }, 1000)
            }, 1000)
        }, 1000)
    </script>
</body>

</html>